package com.jml.跨域;

import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/FromServlet")
public class FromServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("我是get请求。。");
		this.doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("我是post请求/。。。");
		String userName = req.getParameter("userName");
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("userName", userName);

		String jsonpCallback = req.getParameter("jsonpCallback");

		//------------------------------------------------------
		//运行跨域访问，*表示所有的请求，第二个参数是指请求。
		//resp.setHeader("Access-Control-Allow-Origin","*");
		//resp.getWriter().println(jsonObject.toJSONString());
		//------------------------------------------------------
		resp.getWriter().println(jsonpCallback+"("+jsonObject.toJSONString()+")");
	}

}


